<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>examples/Supervised/DeepNetworkTrainingRBM.cpp Source File</title>
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
<script src="../../mlstyle.js"></script>
<link href="../css/besser.css" rel="stylesheet" type="text/css"/>
</head>
<!-- pretty cool: each body gets an id tag which is the basename of the web page  -->
<!--              and allows for page-specific CSS. this is client-side scripted, -->
<!--              so the id will not yet show up in the served source code -->
<script type="text/javascript">
    jQuery(document).ready(function () {
        var url = jQuery(location).attr('href');
        var pname = url.substr(url.lastIndexOf("/")+1, url.lastIndexOf(".")-url.lastIndexOf("/")-1);
        jQuery('#this_url').html('<strong>' + pname + '</strong>');
        jQuery('body').attr('id', pname);
    });
</script>
<body>
    <div id="shark_old">
        <div id="wrap">
            <div id="header">
                <div id="site-name"><a href="../../sphinx_pages/build/html/index.html">Shark machine learning library</a></div>
                <ul id="nav">
                    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/installation.html">Installation</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/tutorials/tutorials.html">Tutorials</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/benchmark.html">Benchmarks</a>
                    </li>
                    <li class="active">
                        <a href="classes.html">Documentation</a>
                        <ul>
                            <li class="first"></li>
                            <li><a href="../../sphinx_pages/build/html/rest_sources/quickref/quickref.html">Quick references</a></li>
                            <li><a href="classes.html">Class list</a></li>
                            <li class="last"><a href="group__shark__globals.html">Global functions</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
<div id="doxywrapper">
<!--
    <div id="global_doxytitle">Doxygen<br>Documentation:</div>
-->
    <div id="navrow_wrapper">
<!-- Generated by Doxygen 1.9.8 -->
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d28a4824dc47e487b107a5db32ef43c4.html">examples</a></li><li class="navelem"><a class="el" href="dir_ca5943d51a26be5f1f9bc4d7d5956bc4.html">Supervised</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">DeepNetworkTrainingRBM.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="_deep_network_training_r_b_m_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="comment">//noisy AutoencoderModel model and deep network</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span><span class="preprocessor">#include &lt;<a class="code" href="_linear_model_8h.html">shark/Models/LinearModel.h</a>&gt;</span><span class="comment">//single dense layer</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span><span class="preprocessor">#include &lt;<a class="code" href="_concatenated_model_8h.html">shark/Models/ConcatenatedModel.h</a>&gt;</span><span class="comment">//for stacking layers and concatenating noise model</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;<a class="code" href="_binary_r_b_m_8h.html">shark/Unsupervised/RBM/BinaryRBM.h</a>&gt;</span> <span class="comment">// model for unsupervised pre-training</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span> </div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span><span class="comment">//training the  model</span></div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span><span class="preprocessor">#include &lt;<a class="code" href="_error_function_8h.html">shark/ObjectiveFunctions/ErrorFunction.h</a>&gt;</span><span class="comment">//the error function performing the regularisation of the hidden neurons</span></div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span><span class="preprocessor">#include &lt;<a class="code" href="_squared_loss_8h.html">shark/ObjectiveFunctions/Loss/SquaredLoss.h</a>&gt;</span> <span class="comment">// squared loss used for unsupervised pre-training</span></div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span><span class="preprocessor">#include &lt;<a class="code" href="_cross_entropy_8h.html">shark/ObjectiveFunctions/Loss/CrossEntropy.h</a>&gt;</span> <span class="comment">// loss used for supervised training</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="preprocessor">#include &lt;<a class="code" href="_zero_one_loss_8h.html">shark/ObjectiveFunctions/Loss/ZeroOneLoss.h</a>&gt;</span> <span class="comment">// loss used for evaluation of performance</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="preprocessor">#include &lt;<a class="code" href="_regularizer_8h.html">shark/ObjectiveFunctions/Regularizer.h</a>&gt;</span> <span class="comment">//L1 and L2 regularisation</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="preprocessor">#include &lt;<a class="code" href="_steepest_descent_8h.html">shark/Algorithms/GradientDescent/SteepestDescent.h</a>&gt;</span> <span class="comment">//optimizer: simple gradient descent.</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="preprocessor">#include &lt;<a class="code" href="_rprop_8h.html">shark/Algorithms/GradientDescent/Rprop.h</a>&gt;</span> <span class="comment">//optimizer for autoencoders</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span> </div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="keyword">using namespace </span>std;</div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespaceshark.html" title="AbstractMultiObjectiveOptimizer.">shark</a>;</div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span> </div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span><span class="comment">//our artificial problem</span></div>
<div class="foldopen" id="foldopen00019" data-start="{" data-end="}">
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"><a class="line" href="_deep_network_training_r_b_m_8cpp.html#a31d564ebeefba38857995d27c8dded2c">   19</a></span><a class="code hl_class" href="classshark_1_1_labeled_data.html" title="Data set for supervised learning.">LabeledData&lt;RealVector,unsigned int&gt;</a> <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#a31d564ebeefba38857995d27c8dded2c">createProblem</a>(){</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span>    std::vector&lt;RealVector&gt; data(320,RealVector(16));</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span>    std::vector&lt;unsigned int&gt; label(320);</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>    RealVector line(4);</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>    <span class="keywordflow">for</span>(std::size_t k = 0; k != 10; ++k){</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>        <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> x=0; x != 16; x++) {</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span>            <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> j=0; j != 4; j++) {</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span>                <span class="keywordtype">bool</span> val = (x &amp; (1&lt;&lt;j)) &gt; 0;</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span>                line(j) = val;</div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>                <span class="keywordflow">if</span>(<a class="code hl_function" href="namespaceshark_1_1random.html#ada5e9e6fd77534e1d99479213e5fed50" title="Flips a coin with probability of heads being pHeads by drawing random numbers from rng.">random::coinToss</a>(<a class="code hl_variable" href="namespaceshark_1_1random.html#ab5c1547eee483974d008d43f621a2234">random::globalRng</a>, 0.3))</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>                    line(j) = !val;</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>            }</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span> </div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i != 4; i++) {</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>                subrange(data[x+k*16],i*4 ,i*4 + 4) = line;</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>            }</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i != 4; i++) {</div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>                <span class="keywordflow">for</span>(<span class="keywordtype">int</span> l=0; l&lt;4; l++) {</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>                    data[x+k*16+160](l*4 + i) = line(l);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span>                }</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>            }</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>            label[x+k*16] = 1; </div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span>            label[x+k*16+160] = 0; </div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>        }</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>    }</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>    <span class="keywordflow">return</span> <a class="code hl_function" href="group__shark__globals.html#ga409b50a287df842bd49e7434a8bbf69e" title="creates a labeled data object from two ranges, representing inputs and labels">createLabeledDataFromRange</a>(data,label);</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>}</div>
</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span> </div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span><span class="comment">//training of an RBM</span></div>
<div class="foldopen" id="foldopen00048" data-start="{" data-end="}">
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"><a class="line" href="_deep_network_training_r_b_m_8cpp.html#a05058126c2d28e9b8008edd10500114d">   48</a></span><a class="code hl_class" href="classshark_1_1_r_b_m.html" title="stub for the RBM class. at the moment it is just a holder of the parameter set and the Energy.">BinaryRBM</a> <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#a05058126c2d28e9b8008edd10500114d">trainRBM</a>(</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>    <a class="code hl_class" href="classshark_1_1_unlabeled_data.html" title="Data set for unsupervised learning.">UnlabeledData&lt;RealVector&gt;</a> <span class="keyword">const</span>&amp; data,<span class="comment">//the data to train with</span></div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>    std::size_t numHidden,<span class="comment">//number of features in the AutoencoderModel</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>    std::size_t iterations, <span class="comment">//number of iterations to optimize</span></div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>    <span class="keywordtype">double</span> regularisation,<span class="comment">//strength of the regularisation</span></div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>    <span class="keywordtype">double</span> learningRate <span class="comment">// learning rate of steepest descent</span></div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>){</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>    <span class="comment">//create rbm with simple binary units using the global random number generator</span></div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>    std::size_t inputs = <a class="code hl_function" href="group__shark__globals.html#ga6231b46b09731352a3cac40709a9625f" title="Return the dimensionality of a dataset.">dataDimension</a>(data);</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>    <a class="code hl_class" href="classshark_1_1_r_b_m.html" title="stub for the RBM class. at the moment it is just a holder of the parameter set and the Energy.">BinaryRBM</a> rbm(<a class="code hl_variable" href="namespaceshark_1_1random.html#ab5c1547eee483974d008d43f621a2234">random::globalRng</a>);</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>    rbm.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a9ef4cbc58af54464387b84111938dd12" title="Creates the structure of the RBM.">setStructure</a>(inputs,numHidden);</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>    <a class="code hl_function" href="group__shark__globals.html#gaa2a8823f1241e854ba858d79fd3e37a2" title="Initialize model parameters uniformly at random.">initRandomUniform</a>(rbm,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));<span class="comment">//initialize weights uniformly</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>    </div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>    <span class="comment">//create derivative to optimize the rbm</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span>    <span class="comment">//we want a simple vanilla CD-1.</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>    <a class="code hl_class" href="classshark_1_1_contrastive_divergence.html" title="Implements k-step Contrastive Divergence described by Hinton et al. (2006).">BinaryCD</a> estimator(&amp;rbm);</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    <a class="code hl_class" href="classshark_1_1_two_norm_regularizer.html" title="Two-norm of the input as an objective function.">TwoNormRegularizer&lt;&gt;</a> regularizer;</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>    <span class="comment">//0.0 is the regularization strength. 0.0 means no regularization. choose as &gt;= 0.0</span></div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span>    estimator.<a class="code hl_function" href="classshark_1_1_contrastive_divergence.html#aee460b5ff7fc5f979bad66fd6d4c0cbc">setRegularizer</a>(regularisation,&amp;regularizer);</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>    estimator.<a class="code hl_function" href="classshark_1_1_contrastive_divergence.html#ad2f9ab74c8ffca4d383827161fa2df90" title="Sets the value of k- the number of steps of the Gibbs Chain.">setK</a>(1);<span class="comment">//number of sampling steps</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>    estimator.<a class="code hl_function" href="classshark_1_1_contrastive_divergence.html#a2471eeb8e6d309b0fa983fdbc5879d9b" title="Sets the training batch.">setData</a>(data);<span class="comment">//the data used for optimization</span></div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>    <span class="comment">//create and configure optimizer</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>    <a class="code hl_class" href="classshark_1_1_steepest_descent.html" title="Standard steepest descent.">SteepestDescent&lt;&gt;</a> optimizer;</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span>    optimizer.<a class="code hl_function" href="classshark_1_1_steepest_descent.html#a68b3feecb0210689c16f1b471e60a9da" title="set learning rate">setLearningRate</a>(learningRate);<span class="comment">//learning rate of the algorithm</span></div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span>    </div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>    <span class="comment">//now we train the rbm and evaluate the mean negative log-likelihood at the end</span></div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> numIterations = iterations;<span class="comment">//iterations for training</span></div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span>    estimator.<a class="code hl_function" href="classshark_1_1_abstract_objective_function.html#abe4776a85c4ce622c25f3290fa1395d1">init</a>();</div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span>    optimizer.<a class="code hl_function" href="classshark_1_1_steepest_descent.html#ad8a11c43e286716c78eddcaad85e2c35">init</a>(estimator);</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>    <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iteration = 0; iteration != numIterations; ++iteration) {</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>        optimizer.<a class="code hl_function" href="classshark_1_1_steepest_descent.html#a481c680541979d1827c1b386203ee4e2" title="updates searchdirection and then does simple gradient descent">step</a>(estimator);</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span>    }</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span>    rbm.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a4412f9b10e320b1db350284a94a4b34d" title="Sets the parameters of the model.">setParameterVector</a>(optimizer.<a class="code hl_function" href="classshark_1_1_abstract_single_objective_optimizer.html#a0909596fcc4f80a8d108859b20b64a81" title="returns the current solution of the optimizer">solution</a>().<a class="code hl_variable" href="structshark_1_1_result_set.html#a5afb306cbdabb9ddb962eb22dbf79bb6">point</a>);</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno">   82</span>    <span class="keywordflow">return</span> rbm;</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span>}</div>
</div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno">   84</span> </div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno">   85</span> </div>
<div class="foldopen" id="foldopen00086" data-start="{" data-end="}">
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"><a class="line" href="_deep_network_training_r_b_m_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">   86</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>()</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>{</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno">   88</span>    <span class="comment">//model parameters</span></div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno">   89</span>    std::size_t numHidden1 = 8;</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno">   90</span>    std::size_t numHidden2 = 8;</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno">   91</span>    <span class="comment">//unsupervised hyper parameters</span></div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno">   92</span>    <span class="keywordtype">double</span> unsupRegularisation = 0.001;</div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno">   93</span>    <span class="keywordtype">double</span> unsupLearningRate = 0.1;</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno">   94</span>    std::size_t unsupIterations = 10000;</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno">   95</span>    <span class="comment">//supervised hyper parameters</span></div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno">   96</span>    <span class="keywordtype">double</span> regularisation = 0.0001;</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno">   97</span>    std::size_t iterations = 200;</div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno">   98</span>    </div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno">   99</span>    <span class="comment">//load data and split into training and test</span></div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno">  100</span>    <a class="code hl_class" href="classshark_1_1_labeled_data.html" title="Data set for supervised learning.">LabeledData&lt;RealVector,unsigned int&gt;</a> data = <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#a31d564ebeefba38857995d27c8dded2c">createProblem</a>();</div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno">  101</span>    data.<a class="code hl_function" href="group__shark__globals.html#ga96ea65352abe5e2c0787e4154a48972f" title="shuffles all elements in the entire dataset (that is, also across the batches)">shuffle</a>();</div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno">  102</span>    <a class="code hl_class" href="classshark_1_1_labeled_data.html" title="Data set for supervised learning.">LabeledData&lt;RealVector,unsigned int&gt;</a> test = <a class="code hl_function" href="group__shark__globals.html#gaa6e44d5e4f847777153927436e61752f" title="Removes the last part of a given dataset and returns a new split containing the removed elements.">splitAtElement</a>(data,<span class="keyword">static_cast&lt;</span>std::size_t<span class="keyword">&gt;</span>(0.5*data.<a class="code hl_function" href="group__shark__globals.html#ga5333445992cd6b14392cd80a1ab5403c" title="Returns the total number of elements.">numberOfElements</a>()));</div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno">  103</span>    </div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno">  104</span>    <span class="comment">//train the first hidden layer</span></div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno">  105</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;pre-training first layer&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno">  106</span>    <a class="code hl_class" href="classshark_1_1_r_b_m.html" title="stub for the RBM class. at the moment it is just a holder of the parameter set and the Energy.">BinaryRBM</a> rbm1 =  <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#a05058126c2d28e9b8008edd10500114d">trainRBM</a>(</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno">  107</span>        data.<a class="code hl_function" href="group__shark__globals.html#ga6f74e657c7e0c8a32b2456fb328bd653" title="Access to inputs as a separate container.">inputs</a>(),numHidden1,</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno">  108</span>        unsupRegularisation,unsupIterations, unsupLearningRate</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno">  109</span>    );</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno">  110</span>    </div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno">  111</span>    <span class="comment">//compute the mapping onto features of the first hidden layer</span></div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno">  112</span>    rbm1.<a class="code hl_function" href="classshark_1_1_r_b_m.html#ad94533d058118a9a0ba544b4c38d9517" title="Sets the type of evaluation, eval will perform.">evaluationType</a>(<span class="keyword">true</span>,<span class="keyword">true</span>);<span class="comment">//we compute the direction visible-&gt;hidden and want the features and no samples</span></div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno">  113</span>    <a class="code hl_class" href="classshark_1_1_unlabeled_data.html" title="Data set for unsupervised learning.">UnlabeledData&lt;RealVector&gt;</a> intermediateData=rbm1(data.<a class="code hl_function" href="group__shark__globals.html#ga6f74e657c7e0c8a32b2456fb328bd653" title="Access to inputs as a separate container.">inputs</a>());</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno">  114</span>    </div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno">  115</span>    <span class="comment">//train the next layer</span></div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno">  116</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;pre-training second layer&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno">  117</span>    <a class="code hl_class" href="classshark_1_1_r_b_m.html" title="stub for the RBM class. at the moment it is just a holder of the parameter set and the Energy.">BinaryRBM</a> rbm2 =  <a class="code hl_function" href="_deep_network_training_r_b_m_8cpp.html#a05058126c2d28e9b8008edd10500114d">trainRBM</a>(</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno">  118</span>        intermediateData,numHidden2,</div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno">  119</span>        unsupRegularisation,unsupIterations, unsupLearningRate</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno">  120</span>    );</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno">  121</span>    </div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno">  122</span> </div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno">  123</span>    <span class="comment">//build three layer neural network from the re-trained RBMs</span></div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno">  124</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;RealVector, LogisticNeuron&gt;</a> layer1(rbm1.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a84024ce828171989645feca12095c3cd" title="Returns the weight matrix connecting the layers.">weightMatrix</a>(),rbm1.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a2c69b9101da84089ff38a8eb3e6b4a9f" title="Returns the layer of hidden neurons.">hiddenNeurons</a>().bias());</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno">  125</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;RealVector, LogisticNeuron&gt;</a> layer2(rbm2.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a84024ce828171989645feca12095c3cd" title="Returns the weight matrix connecting the layers.">weightMatrix</a>(),rbm2.<a class="code hl_function" href="classshark_1_1_r_b_m.html#a2c69b9101da84089ff38a8eb3e6b4a9f" title="Returns the layer of hidden neurons.">hiddenNeurons</a>().bias());</div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno">  126</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;RealVector&gt;</a> output(layer2.<a class="code hl_function" href="classshark_1_1_linear_model.html#a9eeb86bc2b2c822fa5b9617e80a98d91" title="Returns the shape of the output.">outputShape</a>(),<a class="code hl_function" href="group__shark__globals.html#ga1fee3b5830ae11a78109e8c0265c6569" title="Return the number of classes of a set of class labels with unsigned int label encoding.">numberOfClasses</a>(data));</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno">  127</span>    <a class="code hl_function" href="group__shark__globals.html#gaa595fd92ec7d8eebcffd070131b18560" title="Initialize model parameters normally distributed.">initRandomNormal</a>(output,0.01);</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno">  128</span>    <span class="keyword">auto</span> network = layer1 &gt;&gt; layer2 &gt;&gt; output;</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno">  129</span>    </div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno">  130</span>    <span class="comment">//create the supervised problem. Cross Entropy loss with two norm regularisation</span></div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno">  131</span>    <a class="code hl_class" href="classshark_1_1_cross_entropy.html" title="Error measure for classification tasks that can be used as the objective function for training.">CrossEntropy&lt;unsigned int, RealVector&gt;</a> loss;</div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno">  132</span>    <a class="code hl_class" href="classshark_1_1_error_function.html" title="Objective function for supervised learning.">ErrorFunction&lt;&gt;</a> error(data, &amp;network, &amp;loss);</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno">  133</span>    <a class="code hl_class" href="classshark_1_1_two_norm_regularizer.html" title="Two-norm of the input as an objective function.">TwoNormRegularizer&lt;&gt;</a> regularizer(error.<a class="code hl_function" href="classshark_1_1_error_function.html#a398ed5c2c9bb868a8d45e068c5bd245b" title="Accesses the number of variables.">numberOfVariables</a>());</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno">  134</span>    error.<a class="code hl_function" href="classshark_1_1_error_function.html#af786262cd69579e9b26d28de85b8fde9">setRegularizer</a>(regularisation,&amp;regularizer);</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno">  135</span>    </div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno">  136</span>    <span class="comment">//optimize the model</span></div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno">  137</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;training supervised model&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno">  138</span>    <a class="code hl_class" href="classshark_1_1_rprop.html" title="This class offers methods for the usage of the Resilient-Backpropagation-algorithm with/out weight-ba...">Rprop&lt;&gt;</a> optimizer;</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno">  139</span>    error.<a class="code hl_function" href="classshark_1_1_error_function.html#a6ba22ddebbfc72a20503c9089e59abe8">init</a>();</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno">  140</span>    optimizer.<a class="code hl_function" href="classshark_1_1_rprop.html#aa5283be5eb772fcdad29af346c98b498">init</a>(error);</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno">  141</span>    <span class="keywordflow">for</span>(std::size_t i = 0; i != iterations; ++i){</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno">  142</span>        optimizer.<a class="code hl_function" href="classshark_1_1_rprop.html#a9173edb5b7a84bcd46b62a46445754a6">step</a>(error);</div>
<div class="line"><a id="l00143" name="l00143"></a><span class="lineno">  143</span>        std::cout&lt;&lt;i&lt;&lt;<span class="stringliteral">&quot; &quot;</span>&lt;&lt;optimizer.<a class="code hl_function" href="classshark_1_1_abstract_single_objective_optimizer.html#a0909596fcc4f80a8d108859b20b64a81" title="returns the current solution of the optimizer">solution</a>().<a class="code hl_variable" href="structshark_1_1_result_set.html#abfb2c7bc8ee3b184bbef15cb250ead50">value</a>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00144" name="l00144"></a><span class="lineno">  144</span>    }</div>
<div class="line"><a id="l00145" name="l00145"></a><span class="lineno">  145</span>    network.setParameterVector(optimizer.<a class="code hl_function" href="classshark_1_1_abstract_single_objective_optimizer.html#a0909596fcc4f80a8d108859b20b64a81" title="returns the current solution of the optimizer">solution</a>().<a class="code hl_variable" href="structshark_1_1_result_set.html#a5afb306cbdabb9ddb962eb22dbf79bb6">point</a>);</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno">  146</span>    </div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno">  147</span>    <span class="comment">//evaluation</span></div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno">  148</span>    <a class="code hl_class" href="classshark_1_1_zero_one_loss.html" title="0-1-loss for classification.">ZeroOneLoss&lt;unsigned int,RealVector&gt;</a> loss01;</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno">  149</span>    <a class="code hl_class" href="classshark_1_1_data.html" title="Data container.">Data&lt;RealVector&gt;</a> predictionTrain = network(data.<a class="code hl_function" href="group__shark__globals.html#ga6f74e657c7e0c8a32b2456fb328bd653" title="Access to inputs as a separate container.">inputs</a>());</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno">  150</span>    cout &lt;&lt; <span class="stringliteral">&quot;classification error,train: &quot;</span> &lt;&lt; loss01.<a class="code hl_function" href="classshark_1_1_zero_one_loss.html#acba6670d53701d50eed0ecdbc1114175" title="Return zero if labels == predictions and one otherwise.">eval</a>(data.<a class="code hl_function" href="group__shark__globals.html#ga6328a5aa2570c01a5ac5f25076071663" title="Access to labels as a separate container.">labels</a>(), predictionTrain) &lt;&lt; endl;</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno">  151</span>    </div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno">  152</span>    <a class="code hl_class" href="classshark_1_1_data.html" title="Data container.">Data&lt;RealVector&gt;</a> prediction = network(test.<a class="code hl_function" href="group__shark__globals.html#ga6f74e657c7e0c8a32b2456fb328bd653" title="Access to inputs as a separate container.">inputs</a>());</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno">  153</span>    cout &lt;&lt; <span class="stringliteral">&quot;classification error,test: &quot;</span> &lt;&lt; loss01.<a class="code hl_function" href="classshark_1_1_zero_one_loss.html#acba6670d53701d50eed0ecdbc1114175" title="Return zero if labels == predictions and one otherwise.">eval</a>(test.<a class="code hl_function" href="group__shark__globals.html#ga6328a5aa2570c01a5ac5f25076071663" title="Access to labels as a separate container.">labels</a>(), prediction) &lt;&lt; endl;</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno">  154</span>    </div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno">  155</span>}</div>
</div>
</div><!-- fragment --></div><!-- contents -->
</div>
</body>
</html>
